package com.interview.a_多线程.e_Thread和Object中的重要方.f_sleep不会释放锁;

/**
 * sleep 时 不会释放 synchronized
 */
public class Sleep_1 implements Runnable {
    private static int count = 0;
    private static final Object lock = new Object();

    public static void main(String[] args) {
        Sleep_1 sleep_1 = new Sleep_1();

        Thread thread1 = new Thread(sleep_1, "1111");
        thread1.start();

        Thread thread2 = new Thread(sleep_1, "2222");
        thread2.start();

    }

    @Override
    public void run() {
        while (count <= 1) {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + "  获取到锁");
                try {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName() + "  执行完毕\n");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count++;
            }

        }
    }
}
